package com.haikesi.api.mode.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.device.domain.vo.DeviceUpdateVO;
import com.haikesi.api.device.domain.vo.DeviceVO;
import com.haikesi.api.mode.domain.po.PlatformModePO;
import com.haikesi.api.mode.domain.vo.*;
import com.haikesi.api.mode.service.IModeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Api(description = "设备模式相关接口", tags = ApiIndex.ModeController)
@RequestMapping(value = "/api/mode")
@RestController
public class ModeController {

    @Autowired
    private IModeService service;


    @ApiOperation(value = "查询模式列表")
    @GetMapping(value = "/list")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R<PageInfo<List<ModeVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute ModeSearchVO searchVO) {
        List<ModeVO> list = service.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "根据账号查询平台唯一设备")
    @GetMapping(value = "/listByAccount")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<ModeVO>>> listByAccount(@ApiParam(value = "账号") String account) {
        List<ModeVO> list = service.listByAccount(account);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "修改模式")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R update(
            @ApiParam(value = "修改参数") @Valid @RequestBody ModeUpdateVO updateVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.update(updateVO);
        return new R();
    }

    @ApiOperation(value = "添加模式")
    @PostMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R add(
            @ApiParam(value = "添加参数") @Valid @RequestBody ModeAddVO modeAddVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        return service.add(modeAddVO);
    }

    @ApiOperation(value = "删除模式")
    @DeleteMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R delete(@ApiParam(value = "修改参数") @RequestParam String id) {
        service.delete(id);
        return new R();
    }



    @ApiOperation(value = "设置模式")
    @PostMapping(value = "/setMode")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R setMode( @ApiParam(value = "添加参数") @Valid @RequestBody ModeSetVO ModeSetVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.setMode(ModeSetVO);
        return new R();
    }

    @ApiOperation(value = "控制模式")
    @PostMapping(value = "/controlMode")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R controlMode(@ApiParam(value = "模式ID") @RequestParam String id) {
        service.controlMode(id);
        return new R();
    }


    @ApiOperation(value = "查询模式设备列表")
    @GetMapping(value = "/deviceList")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<List<ModeDeviceVO>> list(
            @ApiParam(value = "模式ID") @RequestParam String modeId) {
        List<ModeDeviceVO> list = service.getDeviceList(modeId);
        return new R(list);
    }



    @ApiOperation(value = "添加模式（包含控制一步到位）")
    @PostMapping(value = "addMode")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R addMode(
            @ApiParam(value = "添加参数") @Valid @RequestBody ModeAddSetVO modeAddVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.addMode(modeAddVO);
        return new R();
    }



}
